package cn.itcast.servlet.online_test;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.itcast.service.StudentService;
import cn.itcast.service.TestsService;
import cn.itcast.service.impl.StudentServiceImpl;
import cn.itcast.service.impl.TestsServiceImpl;
import cn.itcast.vo.extend.TestsExtend;
import cn.itcast.vo.query.TestsQuery;

public class FindTestsByTestsType extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String chapterId = request.getParameter("id");
		String tokenId = request.getParameter("token_id");
		String type=request.getParameter("type");
		
		   
		StudentService studentService = new StudentServiceImpl();
		String token_id = studentService.getTokenid(tokenId);
		
		if (token_id == null) {
			JSONObject obj = new JSONObject();
			try {
				obj.put("status", "fail");
				String json = obj.toString();
				System.out.println(json);
				out.write(json);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			TestsService testsService=new TestsServiceImpl();
			TestsQuery tq=new TestsQuery();
			tq.setId(Integer.parseInt(chapterId));
			tq.setType(type);
		
			List<TestsExtend> list=testsService.getTestsByTestsType(tq);
			
			JSONObject obj=new JSONObject();
			try {
				obj.put("status", "success");
				JSONArray testss=new JSONArray();
				for(TestsExtend te:list){
					JSONObject tests=new JSONObject();
					tests.put("id",te.getId());
					tests.put("testsContent",te.getTestsContent());
					tests.put("explain",te.getExplain());
					tests.put("sroce",te.getScore());
					tests.put("testsType",te.getName());
					tests.put("answer", te.getAnswer());
					
				
					JSONArray options=new JSONArray();
					JSONObject optionA=new JSONObject();
					optionA.put("A", te.getOptionA());
					JSONObject optionB=new JSONObject();
					optionB.put("B", te.getOptionB());
					JSONObject optionC=new JSONObject();
					optionC.put("C", te.getOptionC());
					JSONObject optionD=new JSONObject();
					optionD.put("D", te.getOptionD());
					options.put(optionA);
					options.put(optionB);
					options.put(optionC);
					options.put(optionD);
					
					tests.put("options", options);
					
					testss.put(tests);
				}
					obj.put("testss", testss);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String json=obj.toString();
			System.out.println(json);
			out.write(json);
		
		}
		
	}

}
